package com.example.cf.grouplist.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cf.grouplist.Bean.ClassBean;
import com.example.cf.grouplist.R;
import com.example.cf.grouplist.adapter.BaseRecyclerAdapter;
import com.example.cf.grouplist.utils.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

public class ListActivity extends Activity {

    private RecyclerView listView;
    private List<ClassBean> data;
    private BaseRecyclerAdapter adapter;
    private TextView head_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        initData();
        initView();

    }

    private void showMenu(View v,final String name) {
        PopupMenu popupMenu=new PopupMenu(this,v);
        popupMenu.getMenuInflater().inflate(R.menu.production_menu,popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                String s=name+"";
                switch (item.getItemId()){
                    case R.id.putaway:
                        s=name+"上架";
                        break;
                    case R.id.sold_out:
                        s=name+"售罄";
                        break;
                    case R.id.delete:
                        s=name+"删除";
                        break;
                }
                Toast.makeText(ListActivity.this, s, Toast.LENGTH_SHORT).show();

                return true;
            }
        });
        popupMenu.show();
    }

    private void initData() {
        data=new ArrayList<>();
        for (int i=0;i<30;i++){
            ClassBean classBean = new ClassBean();
            classBean.setClassName("品种"+i);
            data.add(classBean);
        }
    }

    private void initView() {
        listView = ((RecyclerView) findViewById(R.id.recyclerView));
//        listView.setLayoutManager(new GridLayoutManager(this,2));
        adapter = new BaseRecyclerAdapter(this,data);
        head_tv = ((TextView) findViewById(R.id.head_tv));

        listView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        listView.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View childViewUnder = listView.findChildViewUnder(head_tv.getMeasuredWidth() / 2, 5);
                if (childViewUnder!=null&&childViewUnder.getContentDescription()!=null){
                    head_tv.setText(childViewUnder.getContentDescription());
                }
                View childViewUnder1 = listView.findChildViewUnder(head_tv.getMeasuredWidth() / 2, head_tv.getMeasuredHeight() + 1);
                if (childViewUnder1!=null&&childViewUnder1.getTag()!=null){
                    int y=-head_tv.getMeasuredHeight()+childViewUnder1.getTop();
                    if (childViewUnder1.getTop()>0)
                        head_tv.setTranslationY(y);
                    else
                        head_tv.setTranslationY(0);
                }else
                    head_tv.setTranslationY(0);

            }
        });

        listView.addItemDecoration(new RecycleViewDivider(this).setmDivider(getResources().getDrawable(R.drawable.divider)));

        adapter.setItemOnLongClickListener(new BaseRecyclerAdapter.ItemOnLongClickListener() {
            @Override
            public void itemOnLongclick(BaseRecyclerAdapter.BaseHolder parent, View item, int pos) {
                   showMenu(item,data.get(pos).getClassName());
            }
        });
        
        adapter.setItemOnclickListener(new BaseRecyclerAdapter.ItemOnclickListener() {
            @Override
            public void itemOnclick(BaseRecyclerAdapter.BaseHolder parent, View item, int pos) {
                Toast.makeText(ListActivity.this, pos + "", Toast.LENGTH_SHORT).show();
            }
        });
        listView.setAdapter(adapter);
    }


}
